package com.ruoyi.uitity;

import okhttp3.*;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

@Component
public class BaiduChatBot {
    private static final String APP_ID = "d59c4819-6268-42a4-ab42-d83d6854ace5";
    private static final String BEARER = "bce-v3/ALTAK-554y1XhyUGtfVswh4wdVB/691a57759e704f8cb54344653965df63eefb205f";
    private static String conversation_id = "";

    private static String cleanAnswer(String answer) {
        // 使用正则表达式替换所有以"^["开头，以"]^"结尾的子串为空字符串
        return answer.replaceAll("\\^\\[.*?\\]\\^", "");
    }

    public static void createConversation() throws IOException {
        OkHttpClient client = new OkHttpClient();
        String url = "https://qianfan.baidubce.com/v2/app/conversation";

        MediaType JSON = MediaType.get("application/json; charset=utf-8");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("app_id", APP_ID);

        RequestBody body = RequestBody.create(JSON, jsonObject.toString());
        Request request = new Request.Builder()
                .url(url)
                .addHeader("Authorization", "Bearer " + BEARER)
                .post(body)
                .build();

        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

            JSONObject responseJson = new JSONObject(response.body().string());
            conversation_id = responseJson.getString("conversation_id");
            System.out.println("Conversation ID: " + conversation_id);
        }
    }
    public  String query(String queryText) throws IOException {
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(30, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .writeTimeout(30, TimeUnit.SECONDS)
                .build();
        String url = "https://qianfan.baidubce.com/v2/app/conversation/runs";

        MediaType JSON = MediaType.get("application/json; charset=utf-8");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("app_id", APP_ID);
        jsonObject.put("query", queryText);
        jsonObject.put("stream", false);
        jsonObject.put("conversation_id", conversation_id);
        jsonObject.put("file_ids", new JSONArray());

        RequestBody body = RequestBody.create(JSON, jsonObject.toString());

        Request request = new Request.Builder()
                .url(url)
                .addHeader("Authorization", "Bearer " + BEARER)
                .post(body)
                .build();
        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
            JSONObject responseJson = new JSONObject(response.body().string());
            String txt = responseJson.getString("answer");
            // 首先替换"^[1]^"
            String txt1 = cleanAnswer(txt);
            // 然后替换"**"
            String answer = txt1.replace("**", "");
            // 返回已经清理过的字符串
            return answer;
        }
    }
}
